<?php

/**
 * 商机列表
 * @Author: wjf
 * @Date: 2022-12-01 18:22:40
 */

namespace app\crm\controller\commercial;

use app\crm\model\client\Ourclients;
use app\crm\model\clue\Followll;
use app\crm\model\commercial\Cllist as CommercialCllist;
use app\crm\model\product\Course;
use think\Controller;
use think\Db;
use think\facade\Session;

class Cllist extends Controller
{
    /**
     * 主页面
     * @Author: wjf
     * @Date: 2022-12-01 09:16:16
     */
    public function index()
    {
        return $this->fetch();
    }

    //遍历数据
    public function list()
    {
        if ($this->request->param('data')) {
            $aa = $this->request->get('data');
            $arr = [];
            foreach ($aa as $k => $v) {
                if (!$v) {
                    unset($aa[$k]);
                }else {
                    $arr[$k]=$v;
                }
            }
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = CommercialCllist::where($arr)->select();
            $count = CommercialCllist::where($arr)->count();
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        }else {
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = CommercialCllist::with('Ourclients')->limit($page, $_GET['limit'])->select();
            $count = CommercialCllist::with('Ourclients')->count();
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        }
        
    }

    //添加页面
    public function add()
    {
        $list = Ourclients::where('status', 2)->where('super_id', 1)->where('classify', 1)->select();
        $this->assign(['list' => $list]);
        return $this->fetch();
    }

    //遍历课程
    public function commodity()
    {
        if ($this->request->isAjax() && $this->request->isGet()) {
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $list  = Course::with('major,campus')->limit($page, $_GET['limit'])->order('id', 'desc')->select();
            foreach ($list as $v) {
                switch ($v['unit']) {
                    case 0:
                        $v['aa'] = '年';
                        break;
                    case 1:
                        $v['aa'] = '季';
                        break;
                    case 2:
                        $v['aa'] = '月';
                        break;
                    case 3:
                        $v['aa'] = '周';
                        break;
                    case 4:
                        $v['aa'] = '天';
                        break;
                    case 5:
                        $v['aa'] = '课时';
                        break;
                    case 6:
                        $v['aa'] = '学期';
                        break;
                }
            }
            $count = Course::with('major,campus')->count();
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' =>  $list]);
        }

        return $this->fetch();
    }

    //存session
    public function colis()
    {
        Session::set('m_id', $_GET['id']);
    }

    //遍历课程
    public function lists()
    {
        if (Session::has('m_id')) {
            $list  = Course::with('major,campus')->order('id', 'desc')->all(Session::pull('m_id') ?? 0);
            foreach ($list as $v) {
                switch ($v['unit']) {
                    case 0:
                        $v['aa'] = '年';
                        break;
                    case 1:
                        $v['aa'] = '季';
                        break;
                    case 2:
                        $v['aa'] = '月';
                        break;
                    case 3:
                        $v['aa'] = '周';
                        break;
                    case 4:
                        $v['aa'] = '天';
                        break;
                    case 5:
                        $v['aa'] = '课时';
                        break;
                    case 6:
                        $v['aa'] = '学期';
                        break;
                }
            }
            return json(['code' => 0, 'msg' => '遍历', 'data' =>  $list]);
        } else {
            return json(['code' => 0, 'msg' => '遍历', 'count' => 0, 'data' =>  '']);
        }
    }

    //添加
    public function adds()
    {
        // 启动事务
        Db::startTrans();
        if ($this->request->isAjax() && $this->request->isPost()) {
            $CommercialCllist = new CommercialCllist(); 
            try {
                $list = $_POST['tract'];
                foreach ($_POST['commodity'] as $v) {
                    $list['course_id'] = $v['course_id'];   
                    $CommercialCllist->save($list);
                }   
                // 提交事务
                Db::commit();
                return json(['code' => 0, 'msg' => '添加成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 1, 'msg' => '添加失败']);
            }
        }
    }

    //跟新遍历
    public function edit()
    {
        Session::set('e_id', $_GET['id']);
        $data = CommercialCllist::with('Ourclients')->where('id',$_GET['id'])->select();
        $aa = CommercialCllist::with('Ourclients')->select();
        $this->assign(['list' => $data, 'aa' => $aa]);
        return $this->fetch();  
    }

    //跟新
    public function amend()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $data = $this->request->post();
            $CommercialCllist = new CommercialCllist;
            if ($CommercialCllist->allowField(true)->isUpdate(true)->save($data, ['id' => Session::pull('e_id')])) {
                return json(['code' => 1, 'msg' => '跟新成功']);
            } else {
                return json(['code' => 0, 'msg' => '跟新失败']);
            }
        }
    }

    //跟进
    public function followup()
    {
        if (!empty($_GET['id'])) {
            Session::set('f_id', $_GET['id']);
            $list = CommercialCllist::where('id',$_GET['id'])->select();
            $this->assign(['list' => $list]);
        }
        return $this->fetch();
        
    }

    //跟进数据
    public function foop()
    {
        if ($this->request->isGet() && $this->request->isAjax()) {       
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = Followll::limit($page,$_GET['limit'])->where('classify_id',Session::get('f_id'))->where('classify', 2)->select();
            $count = Followll::all()->count();
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        }
    }

    //添加跟进数据
    public function fpdata()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $data = $this->request->post();
            $data['classify_id'] = Session::get('f_id');
            $Follow = new Followll;
            if ($Follow->save($data)) {
                return json(['code' => 1, 'msg' => '跟进成功']);
            } else {
                return json(['code' => 0, 'msg' => '跟进失败']);
            }
        }
    }
}
